home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch13 / Distort.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-20  |  6KB  |  200 lines

  1. VERSION 5.00
  2. Begin VB.Form frmDistort 
  3.    Caption         =   "Distort"
  4.    ClientHeight    =   6270
  5.    ClientLeft      =   1710
  6.    ClientTop       =   465
  7.    ClientWidth     =   5910
  8.    KeyPreview      =   -1  'True
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   6270
  12.    ScaleWidth      =   5910
  13.    Begin VB.OptionButton optDistortion 
  14.       Caption         =   "Twist"
  15.       Height          =   255
  16.       Index           =   3
  17.       Left            =   3240
  18.       TabIndex        =   4
  19.       Top             =   0
  20.       Width           =   975
  21.    End
  22.    Begin VB.OptionButton optDistortion 
  23.       Caption         =   "Sines"
  24.       Height          =   255
  25.       Index           =   2
  26.       Left            =   2160
  27.       TabIndex        =   3
  28.       Top             =   0
  29.       Width           =   975
  30.    End
  31.    Begin VB.OptionButton optDistortion 
  32.       Caption         =   "Wave"
  33.       Height          =   255
  34.       Index           =   1
  35.       Left            =   1080
  36.       TabIndex        =   2
  37.       Top             =   0
  38.       Width           =   975
  39.    End
  40.    Begin VB.OptionButton optDistortion 
  41.       Caption         =   "None"
  42.       Height          =   255
  43.       Index           =   0
  44.       Left            =   0
  45.       TabIndex        =   1
  46.       Top             =   0
  47.       Width           =   975
  48.    End
  49.    Begin VB.PictureBox picCanvas 
  50.       AutoRedraw      =   -1  'True
  51.       Height          =   5895
  52.       Left            =   0
  53.       ScaleHeight     =   -800
  54.       ScaleLeft       =   -400
  55.       ScaleMode       =   0  'User
  56.       ScaleTop        =   400
  57.       ScaleWidth      =   800
  58.       TabIndex        =   0
  59.       Top             =   360
  60.       Width           =   5895
  61.    End
  62. Attribute VB_Name = "frmDistort"
  63. Attribute VB_GlobalNameSpace = False
  64. Attribute VB_Creatable = False
  65. Attribute VB_PredeclaredId = True
  66. Attribute VB_Exposed = False
  67. Option Explicit
  68. ' Location of viewing eye.
  69. Private EyeR As Single
  70. Private EyeTheta As Single
  71. Private EyePhi As Single
  72. ' Location of focus point.
  73. Private Const FocusX = 0#
  74. Private Const FocusY = 0#
  75. Private Const FocusZ = 0#
  76. Private Projector(1 To 4, 1 To 4) As Single
  77. Private Polylines As Collection
  78. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  79. Const Dtheta = PI / 20
  80.     Select Case KeyCode
  81.         Case vbKeyLeft
  82.             EyeTheta = EyeTheta - Dtheta
  83.         
  84.         Case vbKeyRight
  85.             EyeTheta = EyeTheta + Dtheta
  86.         
  87.         Case vbKeyUp
  88.             EyePhi = EyePhi - Dtheta
  89.         
  90.         Case vbKeyDown
  91.             EyePhi = EyePhi + Dtheta
  92.         
  93.         Case Else
  94.             Exit Sub
  95.     End Select
  96.     DrawData picCanvas
  97. End Sub
  98. Private Sub Form_Load()
  99.     Me.Show
  100.     MousePointer = vbHourglass
  101.     DoEvents
  102.     ' Initialize the eye position.
  103.     EyeR = 1500
  104.     EyeTheta = PI * 0.17
  105.     EyePhi = PI * 0.16
  106.     ' Start with no distortion.
  107.     optDistortion(0).value = True
  108.     MousePointer = vbDefault
  109. End Sub
  110. ' Create some polylines to display.
  111. Private Sub CreateData()
  112. Const GAP = 25
  113. Dim i As Single
  114. Dim j As Single
  115. Dim poly As Polyline3d
  116.     ' Create the polyline collection.
  117.     Set Polylines = New Collection
  118.     ' Create the top (perpendicular to Y axis).
  119.     Set poly = New Polyline3d
  120.     Polylines.Add poly
  121.     For i = -200 To 200 Step GAP
  122.         For j = -200 To 200 - GAP Step GAP
  123.             poly.AddSegment i, 200, j, i, 200, j + GAP
  124.             poly.AddSegment j, 200, i, j + GAP, 200, i
  125.         Next j
  126.     Next i
  127.     ' Create the front (perpendicular to Z axis).
  128.     Set poly = New Polyline3d
  129.     Polylines.Add poly
  130.     For i = -200 To 200 Step GAP
  131.         For j = -200 To 200 - GAP Step GAP
  132.             poly.AddSegment i, j, 200, i, j + GAP, 200
  133.             poly.AddSegment j, i, 200, j + GAP, i, 200
  134.         Next j
  135.     Next i
  136.     ' Create the side (perpendicular to X axis).
  137.     Set poly = New Polyline3d
  138.     Polylines.Add poly
  139.     For i = -200 To 200 Step GAP
  140.         For j = -200 To 200 - GAP Step GAP
  141.             poly.AddSegment 200, i, j, 200, i, j + GAP
  142.             poly.AddSegment 200, j, i, 200, j + GAP, i
  143.         Next j
  144.     Next i
  145. End Sub
  146. ' Display the data.
  147. Private Sub DrawData(ByVal pic As PictureBox)
  148. Dim pline As Polyline3d
  149. Dim trans As Distortion
  150. Dim trans_sines As DistortSines
  151. Dim trans_twist As DistortTwist
  152. Dim trans_circle As DistortCircle
  153.     Screen.MousePointer = vbHourglass
  154.     pic.Cls
  155.     DoEvents
  156.     ' Recreate the data.
  157.     CreateData
  158.     ' Load the correct transformation.
  159.     If optDistortion(1).value Then
  160.         ' Circle.
  161.         Set trans = New DistortCircle
  162.         Set trans_circle = trans
  163.         trans_circle.amplitude = 12.5
  164.         trans_circle.period = 150
  165.     ElseIf optDistortion(2).value Then
  166.         ' Sines.
  167.         Set trans = New DistortSines
  168.         Set trans_sines = trans
  169.         trans_sines.amplitude = 30
  170.         trans_sines.period = 150
  171.     ElseIf optDistortion(3).value Then
  172.         ' Twist.
  173.         Set trans = New DistortTwist
  174.         Set trans_twist = trans
  175.         trans_twist.Cx = 0
  176.         trans_twist.Cz = 0
  177.         trans_twist.offset = -100
  178.         trans_twist.period = 500 * PI
  179.     End If
  180.     ' Apply the transformation.
  181.     If Not (trans Is Nothing) Then
  182.         For Each pline In Polylines
  183.             pline.Transform trans
  184.         Next pline
  185.     End If
  186.     ' Build the projection transformation.
  187.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  188.     ' Transform and draw the polylines.
  189.     pic.Cls
  190.     For Each pline In Polylines
  191.         pline.ApplyFull Projector
  192.         pline.Draw pic
  193.     Next pline
  194.     Screen.MousePointer = vbDefault
  195. End Sub
  196. ' Display the data with the new transformation.
  197. Private Sub optDistortion_Click(Index As Integer)
  198.     DrawData picCanvas
  199. End Sub
  200.